package com.sevenstudio.adu.gasing.state;

import org.andengine.engine.Engine.EngineLock;
import org.andengine.entity.modifier.MoveYModifier;
import org.andengine.entity.modifier.ScaleModifier;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.entity.scene.Scene;
import org.andengine.input.touch.TouchEvent;

import android.view.KeyEvent;

import com.sevenstudio.adu.gasing.define.GameEngineConfiguration;
import com.sevenstudio.adu.gasing.bluetooth.BT_Server_Client;
import com.sevenstudio.adu.gasing.define.iGameDefines;
import com.sevenstudio.adu.gasing.element.ABackground;
import com.sevenstudio.adu.gasing.element.AMusic;
import com.sevenstudio.adu.gasing.element.ARectangle;
import com.sevenstudio.adu.gasing.element.AScene;
import com.sevenstudio.adu.gasing.element.ASound;
import com.sevenstudio.adu.gasing.element.ASprite;
import com.sevenstudio.adu.gasing.element.AText;
import com.sevenstudio.adu.gasing.engine.Anchor;
import com.sevenstudio.adu.gasing.engine.GameEngine;
import com.sevenstudio.adu.gasing.engine.Utils;

public class State_Main_Menu {

	GameEngine engine;
	AScene scene;
	AText text;
	ABackground bg_Main_menu;
	ASprite btn_Play, btn_Setting, logo_setting, txt_setting , btn_exit, bg_setting;
	
	ASprite[] btn_Sub_Setting = new ASprite[5];
	
	static AMusic bgm_menu;
	ASound sfx_Yes, sfx_No;
	
	boolean Down = false, Up = false , close = false;
	
	final float DURATION = 0.3f;
	
	float scale =0;
	ASprite bg_Close_Notif;
	ASprite x;
	ASprite v;
	
	ARectangle rec;
	
	
	private int INDEX_BTN_SOUND = 0;
	private int INDEX_BTN_CREDITS = 1;
	private int INDEX_BTN_HELP = 2;
	private int INDEX_BTN_HELP_OF = 3;
	private int INDEX_BTN_SOUND_OF = 4;
	

	
	public State_Main_Menu(GameEngine engine)
	{
		this.engine = engine;
		scene = new AScene(0);
		
		bgm_menu = new AMusic(engine, iGameDefines.BGM_MENU, 0);
		sfx_Yes = new ASound(engine, iGameDefines.SFX_YES);
		sfx_No = new ASound(engine, iGameDefines.SFX_NO);
		
		bg_Main_menu = new ABackground(engine, iGameDefines.SPRITE_MENU, iGameDefines.INDEX_BG_MENU);
		
		bg_Close_Notif = new ASprite(engine, iGameDefines.SPRITE_MENU, iGameDefines.INDEX_BG_CLOSE);
		bg_Close_Notif.theSprite().setVisible(false);
		x = new ASprite(engine, iGameDefines.SPRITE_MENU, 13);
		x.theSprite().setVisible(false);
		v = new ASprite(engine, iGameDefines.SPRITE_MENU, 14);
		v.theSprite().setVisible(false);
		rec = new ARectangle(engine, iGameDefines.RECT_GAME, 6);
		rec.theRect().setAlpha(100);
		rec.theRect().setVisible(false);
		
		
		btn_Play = new ASprite(engine, iGameDefines.SPRITE_MENU, iGameDefines.INDEX_BTN_MENU_PLAY);
		btn_Play.SetPos(Utils.GetAnchorX(btn_Play.theSprite().getWidth(), Anchor.CENTER), Utils.GetRatioHeight(150 * 2));
		
		btn_Setting = new ASprite(engine, iGameDefines.SPRITE_MENU, iGameDefines.INDEX_BTN_MENU_SETTING);
		btn_Setting.SetPos(Utils.GetRatioWidth(10 * 2.5f), Utils.GetRatioHeight(170 * 2));
		
		logo_setting = new ASprite(engine, iGameDefines.SPRITE_MENU, iGameDefines.INDEX_BTN_MENU_SET_LOGO);
		logo_setting.SetPos(Utils.getCenterObjX(btn_Setting.theSprite(), logo_setting.theSprite()),
				Utils.getCenterObjY(btn_Setting.theSprite(), logo_setting.theSprite()) + Utils.GetRatioHeight(5 * 2));
		
		btn_exit = new ASprite(engine, iGameDefines.SPRITE_MENU, iGameDefines.INDEX_BTN_EXIT);
		btn_exit.SetPos(State_Main_Activity.CAMERA_WIDTH - (Utils.GetRatioWidth(10 * 2.5f) + btn_exit.theSprite().getWidth()), Utils.GetRatioHeight(170 * 2));
		
		txt_setting = new ASprite(engine, iGameDefines.SPRITE_MENU, iGameDefines.INDEX_TXT_SETTING);
		txt_setting.SetPos(Utils.getCenterObjX(btn_Setting.theSprite(), txt_setting.theSprite()), 
				btn_Setting.GetY());
		
		bg_setting = new ASprite(engine, iGameDefines.SPRITE_MENU, iGameDefines.INDEX_BG_SETTING);
		bg_setting.SetPos(btn_Setting.GetX(), btn_Setting.GetY());
		bg_setting.theSprite().setScaleY(0f);
		
		for(int i=0; i < btn_Sub_Setting.length; i++)
		{
			btn_Sub_Setting[i] = new ASprite(engine, iGameDefines.SPRITE_MENU, i+4);
			
			if(i > 2)
			{
				btn_Sub_Setting[i].theSprite().setVisible(false);
				
			}
			
		}
		
		scene.theScene().setOnAreaTouchListener(engine);
		scene.theScene().setOnSceneTouchListener(engine);
		scene.theScene().registerTouchArea(x.theSprite());
		scene.theScene().registerTouchArea(v.theSprite());
		scene.theScene().registerTouchArea(btn_Sub_Setting[INDEX_BTN_CREDITS].theSprite());
		scene.theScene().registerTouchArea(btn_Sub_Setting[INDEX_BTN_SOUND].theSprite());

		scene.theScene().registerTouchArea(btn_Sub_Setting[INDEX_BTN_SOUND_OF].theSprite());
		scene.theScene().registerTouchArea(btn_Sub_Setting[INDEX_BTN_HELP].theSprite());
		scene.theScene().registerTouchArea(btn_Sub_Setting[INDEX_BTN_HELP_OF].theSprite());
		

	}
	public void Init() {
		
		if(BT_Server_Client.isBluetooth)
//		BT_Server_Client.stopBTThread();
		
		scene.theScene().detachChildren();
		bg_setting.theSprite().detachChildren();
		bg_Close_Notif.theSprite().detachChildren();

		BT_Server_Client.isBluetooth = false;
		
		if(GameEngineConfiguration.is_sound_enable)
		{

		bgm_menu.Play();
		}
		engine.SetGameScene(scene);
		engine.SceneAttach(scene, bg_Main_menu);
		engine.SceneAttach(scene, btn_Play);
		
		engine.SceneAttach(scene, bg_setting);
		
		for(int i=0; i < btn_Sub_Setting.length; i++)
		{
			engine.attachParent(bg_setting, btn_Sub_Setting[i]);
		}
		
			
		Utils.TRACE("POS Y "+btn_Sub_Setting[0].GetY());
		Utils.TRACE("POS11 Y "+btn_Sub_Setting[1].GetY());
		
		engine.SceneAttach(scene, btn_Setting);
		engine.SceneAttach(scene, logo_setting);
		engine.SceneAttach(scene, txt_setting);
		engine.SceneAttach(scene, btn_exit);
		
		engine.SceneAttach(scene, rec);
		engine.SceneAttach(scene, bg_Close_Notif);

		engine.attachParent(bg_Close_Notif, x);
		engine.attachParent(bg_Close_Notif, v);
	
	
							
	}
	
	public void Update() 
	{
		if(btn_Sub_Setting[INDEX_BTN_SOUND].theSprite().isVisible())
		{
			GameEngineConfiguration.is_sound_enable = true;
		}
		else if(btn_Sub_Setting[INDEX_BTN_SOUND_OF].theSprite().isVisible())
		{
			GameEngineConfiguration.is_sound_enable = false;
		}
		
		if(btn_Sub_Setting[INDEX_BTN_HELP].theSprite().isVisible())
		{
			GameEngineConfiguration.is_tutor_enable = true;
		}
		else if(btn_Sub_Setting[INDEX_BTN_HELP_OF].theSprite().isVisible())
		{
			GameEngineConfiguration.is_tutor_enable = false;
		}
	
	}
	
	public void keyBack(int keyCode, KeyEvent event)
	{
		if(keyCode == KeyEvent.KEYCODE_BACK)
		{
			

			visibleClose();
			
		}
		
	}
	public void Touch(TouchEvent pSceneTouchEvent, ITouchArea pTouchArea,
			float pTouchAreaLocalX, float pTouchAreaLocalY) 
	{
		if(pSceneTouchEvent.isActionDown() && pTouchArea.equals(x.theSprite()) && x.theSprite().isVisible())
		{
			disableClose();
		}
		
		if(pSceneTouchEvent.isActionDown() && pTouchArea.equals(v.theSprite()) && v.theSprite().isVisible())
		{
			engine.exit();
//			engine.SetNextMessageState(0);
		}
		
		if(pSceneTouchEvent.isActionDown() && pTouchArea.equals(btn_Sub_Setting[INDEX_BTN_CREDITS].theSprite()))
		{
			downMenu();
			exit();
			engine.SetNextMessageState(0);
			engine.SetNextGameState(iGameDefines.GAME_STATE_ABOUT);
		}
		
		else if(pSceneTouchEvent.isActionDown() && pTouchArea.equals(btn_Sub_Setting[INDEX_BTN_SOUND].theSprite())
				&& GameEngineConfiguration.is_sound_enable && btn_Sub_Setting[INDEX_BTN_SOUND].theSprite().isVisible())
		{
			bgm_menu.Pause();
			btn_Sub_Setting[INDEX_BTN_SOUND].theSprite().setVisible(false);
			btn_Sub_Setting[INDEX_BTN_SOUND_OF].theSprite().setVisible(true);
			
		}
		else if(pSceneTouchEvent.isActionDown() && pTouchArea.equals(btn_Sub_Setting[INDEX_BTN_SOUND_OF].theSprite())
				&& !GameEngineConfiguration.is_sound_enable && btn_Sub_Setting[INDEX_BTN_SOUND_OF].theSprite().isVisible())
		{
			bgm_menu.Play();
			btn_Sub_Setting[INDEX_BTN_SOUND].theSprite().setVisible(true);
			btn_Sub_Setting[INDEX_BTN_SOUND_OF].theSprite().setVisible(false);
			
		}
		else if(pSceneTouchEvent.isActionDown() && pTouchArea.equals(btn_Sub_Setting[INDEX_BTN_HELP].theSprite())
			&& GameEngineConfiguration.is_tutor_enable	&& btn_Sub_Setting[INDEX_BTN_HELP].theSprite().isVisible())
		{
						
				btn_Sub_Setting[INDEX_BTN_HELP].theSprite().setVisible(false);
				btn_Sub_Setting[INDEX_BTN_HELP_OF].theSprite().setVisible(true);
						
			Utils.TRACE("TUTOR RR");
		}
		else if(pSceneTouchEvent.isActionDown() && pTouchArea.equals(btn_Sub_Setting[INDEX_BTN_HELP_OF].theSprite())
				&& !GameEngineConfiguration.is_tutor_enable	&& btn_Sub_Setting[INDEX_BTN_HELP_OF].theSprite().isVisible())
		{
			
				btn_Sub_Setting[INDEX_BTN_HELP].theSprite().setVisible(true);
				btn_Sub_Setting[INDEX_BTN_HELP_OF].theSprite().setVisible(false);
			
		}
		
		
		
		
	}
	public void SceneTouch(Scene arg0, TouchEvent arg1) {
		
		if(arg1.isActionDown() && Utils.IsTouched_Down(btn_Play, arg1) && !close)
		{
			btn_Play.setScale(1.1f);
			sfx_Yes.Play();

		}
		else if(arg1.isActionUp() && Utils.IsTouched_Up(btn_Play, arg1) && !close)
		{
			downMenu();
			btn_Play.setScale(1f);
			engine.SetNextMessageState(engine.NextMessageState());

			

//			GameEngine.context.runOnUiThread(new Runnable()
//			{
//				public void run()
//				{
//			Bluetooth();
//				}
//			});

		}
		if(arg1.isActionDown() && Utils.IsTouched_Down(btn_Setting, arg1) && !close)
		{
			btn_Setting.setScale(1.1f);
			logo_setting.setScale(1.1f);
			
			
			if(bg_setting.GetY() == btn_Setting.GetY())
			{
				bg_setting.theSprite().registerEntityModifier(new MoveYModifier(DURATION, bg_setting.GetY(), Utils.GetRatioHeight(10)));
				txt_setting.theSprite().registerEntityModifier(new MoveYModifier(DURATION, txt_setting.GetY(), Utils.GetRatioHeight(20)));
		
				bg_setting.theSprite().registerEntityModifier(new ScaleModifier(DURATION, bg_setting.theSprite().getScaleX(), bg_setting.theSprite().getScaleX(), bg_setting.theSprite().getScaleY(), 1f));

				Up = true;
				Down = false;
				engine.rotateObj(logo_setting, 1);
				sfx_Yes.Play();
			}
			
			if(bg_setting.theSprite().getY() == Utils.GetRatioHeight(10))
			{
				bg_setting.theSprite().registerEntityModifier(new MoveYModifier(DURATION, bg_setting.GetY(), btn_Setting.GetY()));
				txt_setting.theSprite().registerEntityModifier(new MoveYModifier(DURATION, txt_setting.GetY(), btn_Setting.GetY()));
				
				bg_setting.theSprite().registerEntityModifier(new ScaleModifier(DURATION, bg_setting.theSprite().getScaleX(), bg_setting.theSprite().getScaleX(), bg_setting.theSprite().getScaleY(), 0f));
		
				Down = true;
				Up = false;
				engine.rotateObj(logo_setting, -1);
				sfx_No.Play();
			}
						
		}
		else if(arg1.isActionUp() && Utils.IsTouched_Up(btn_Setting, arg1) && !close)
		{
		
			btn_Setting.setScale(1f);
			logo_setting.setScale(1f);
		}
		if(arg1.isActionDown() && Utils.IsTouched_Down(btn_exit, arg1) && !close)
		{
			btn_exit.setScale(1.1f);
			sfx_No.Play();
		}
		else if(arg1.isActionUp() && Utils.IsTouched_Up(btn_exit, arg1) && !close)
		{
		
			btn_exit.setScale(1f);
			visibleClose();
		}
		
		
	}
	
	private void downMenu()
	{
		if(Up)
		{
			bg_setting.theSprite().setPosition(bg_setting.GetX(), btn_Setting.GetY());//registerEntityModifier(new MoveYModifier(DURATION, bg_setting.GetY(), btn_Setting.GetY()));
			txt_setting.theSprite().setPosition(txt_setting.GetX(), btn_Setting.GetY());//.registerEntityModifier(new MoveYModifier(DURATION, txt_setting.GetY(), btn_Setting.GetY()));
	
			bg_setting.theSprite().setScaleY(0);//registerEntityModifier(new ScaleModifier(DURATION, bg_setting.theSprite().getScaleX(), bg_setting.theSprite().getScaleX(), bg_setting.theSprite().getScaleY(), 0f));
	
			Down = true;
			Up = false;
		}
	}
	
	public void exit() 
	{
		detachMenu();
		Utils.TRACE("Detach "+engine.gameCurrentState);
	}
	
	void detachMenu()
	{
		final EngineLock lock = engine.getEngine().getEngineLock();
		lock.lock();
		
				
		bg_Main_menu.DetachSelf();
		btn_Play.DetachSelf();	
		btn_Setting.DetachSelf();
		logo_setting.DetachSelf();
		btn_exit.DetachSelf();
		bg_setting.DetachSelf();
		
		for(int i=0; i < btn_Sub_Setting.length; i++)
		{
			btn_Sub_Setting[i].DetachSelf();
		}
		
		txt_setting.DetachSelf();
		
		rec.theRect().detachSelf();		
		bg_Close_Notif.DetachSelf();
		x.DetachSelf();
		v.DetachSelf();
		
		lock.unlock();
	}
	
	void visibleClose()
	{
		close = true;
		bg_Close_Notif.theSprite().setVisible(true);
		x.theSprite().setVisible(true);
		v.theSprite().setVisible(true);
		rec.theRect().setVisible(true);
	}
	
	void disableClose()
	{
		close = false;
		bg_Close_Notif.theSprite().setVisible(false);
		x.theSprite().setVisible(false);
		v.theSprite().setVisible(false);
		rec.theRect().setVisible(false);
	}
	
//	private void Bluetooth()
//	{
//		engine.server_Client = new BT_Server_Client(engine);
//	}

	
	
	

}
